﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DCT.CodeBox.Utility
{
    /// <summary>
    /// 字符串相关类
    /// </summary>
    public static class StringUtility
    {
        /// <summary>
        /// 把任何对象转化为字符串，不用判断是否为Null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(object obj)
        {
            return ToString(obj, "");
        }

        /// <summary>
        /// 把任何对象转化为字符串，不用判断是否为Null
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultString">如果为null时，返回的数字</param>
        /// <returns></returns>
        public static string ToString(object obj, string defaultString)
        {
            if (obj == null || obj is DBNull) {
                return defaultString;
            }
            return obj.ToString();
        }

        /// <summary>
        /// 生成随机码
        /// </summary>
        /// <param name="length">随机码长度</param>
        /// <param name="useNum">是否有数据</param>
        /// <param name="useLow">是否有小写字母</param>
        /// <param name="useUpp">是否有大写字母</param>
        /// <param name="useSpe">是否有特殊字符</param>
        /// <param name="custom">其它定义的密码字符</param>
        /// <returns></returns>
        public static string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNum == true) { str += "0123456789"; }
            if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        }

        /// <summary>
        /// 转换连续字符串为日志，格式为yyyyMMddHHmmss
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ConvertContinuationStringToDateTime(string str)
        {
            return ConvertContinuationStringToDateTime(str, DateTime.Now);
        }

        /// <summary>
        /// 转换连续字符串为日志，格式为yyyyMMddHHmmss
        /// </summary>
        /// <param name="str"></param>
        /// <param name="failedDefault"></param>
        /// <returns></returns>
        public static DateTime ConvertContinuationStringToDateTime(string str, DateTime failedDefault)
        {
            DateTime time = failedDefault;
            if (str != null && str.Length == 14)
            {
                bool allInt = true;
                foreach (char c in str)
                {
                    if (!char.IsDigit(c)) { allInt = false; break; }
                }

                if (allInt)
                {
                    int year = Convert.ToInt32(str.Substring(0, 4));
                    int month = Convert.ToInt32(str.Substring(4, 2));
                    int day = Convert.ToInt32(str.Substring(6, 2));
                    int hour = Convert.ToInt32(str.Substring(8, 2));
                    int minute = Convert.ToInt32(str.Substring(10, 2));
                    int second = Convert.ToInt32(str.Substring(12, 2));

                    try
                    {
                        time = new DateTime(year, month, day, hour, minute, second);
                    }
                    catch
                    {
                        time = failedDefault;
                    }
                }
            }

            return time;
        }

    }
}
